﻿using System;
using System.Drawing;
using System.IO;
using UselessExplorer.UI;

namespace UselessExplorer.Controller
{
    class FileExplorerController
    {
        DirectoryTree dirTree = null;

        DirectoryInfo[] m_Folders;

        public DirectoryInfo[] Folders
        {
            get { return m_Folders; }
            set { m_Folders = value; }
        }
        FileInfo[] m_Files;

        public FileInfo[] Files
        {
            get { return m_Files; }
            set { m_Files = value; }
        }
        

        private static FileExplorerController instance;
        public static FileExplorerController Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FileExplorerController();
                }
                return instance;
            }
        }

        public FileExplorerController ()
        {
            dirTree = new DirectoryTree();
        }




        public DirectoryTree InitLoad ( string DriveName )
        {
            dirTree.Drive = DriveName.Substring( 0, 2 );
            dirTree.Location = new Point( 5, 5 );
            dirTree.ShowPlusMinus = false;

            return dirTree;
        }

        /// <summary>
        /// Adding the Extension of the file in order to view the contents of the file
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public ClsGlobal.StatusExtnAdd AddExtension ( string filePath )
        {
            DirectoryInfo dirInfo = new DirectoryInfo(filePath);

            if (!ClsGlobal.Instance.IsExtensionRestricted( dirInfo.Extension ))
            {
                int extensionCount = ClsGlobal.Instance.Extensions.Length;
                string[] tempList = new string[extensionCount + 1];
                string[] temp = ClsGlobal.Instance.Extensions;

                for (int i = 0; i < extensionCount; i++)
                {
                    tempList[i] = temp[i];
                }

                tempList[extensionCount] = dirInfo.Extension.ToLower();
                ClsGlobal.Instance.Extensions = tempList;

                if (ClsGlobal.Instance.SaveExtensions() == ClsGlobal.StatusExtnAdd.Failed)
                {
                    return ClsGlobal.StatusExtnAdd.Failed;
                }
                else
                {
                    return ClsGlobal.StatusExtnAdd.ExtensionAdded;
                }
            }
            else
            {
                return ClsGlobal.StatusExtnAdd.Restricted;
            }
        }

        /// <summary>
        /// Loads all the Folders and Files for the specified directory
        /// </summary>
        /// <param name="path"></param>
        /// <param name="isRootNode"></param>
        public void LoadData ( string path, bool isRootNode )
        {
            DirectoryInfo currentDir = new DirectoryInfo( path );

            try
            {
                m_Folders = currentDir.GetDirectories();
                m_Files = currentDir.GetFiles();
            }
            catch (FileNotFoundException fnfe)
            {
                throw fnfe;
            }
            catch (UnauthorizedAccessException uae)
            {
                throw uae;
            }
            catch (Exception e)
            {
                
            }
        }

        /// <summary>
        /// Deletes the folder or file from the file system
        /// </summary>
        /// <param name="FilePath"></param>
        public void DeleteFolderOrFile ( string FolderorFilePath )
        {
            if (IsDirectory( FolderorFilePath ))
            {
                DirectoryInfo dirInfo = new DirectoryInfo( FolderorFilePath );
                dirInfo.Delete( true );
            }
            else
            {
                FileInfo fi = new FileInfo( FolderorFilePath );
                fi.Delete();
            }
        }

        public bool IsDirectory ( string path )
        {
            return Directory.Exists( path );
        }
    }
}
